package com.jwthhealth_pub.app.view;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.app.App;
import com.jwthhealth_pub.app.module.ThraPreviewModule;
import com.jwthhealth_pub.common.base.BaseActivity;
import com.jwthhealth_pub.common.preference.PreferenceKey;
import com.jwthhealth_pub.old.api.ApiHelper;
import com.jwthhealth_pub.old.sign.model.LoginModel;
import com.jwthhealth_pub.utils.Contas;

import butterknife.BindArray;
import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.jwthhealth_pub.common.LogUtil.makeLogTag;

/**
 * main activity
 * Created by liuzhen on 2017/12/4.
 */

public class MainActivity extends BaseActivity {

    private static final String TAG = makeLogTag(MainActivity.class);

    @BindView(R.id.vp_main)
    ViewPager vpMain;
    @BindView(R.id.tl_main)
    TabLayout tlMain;

    @BindArray(R.array.mainTabs)
    String[] mainTabs;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        getDefaultInfo();

        getThraPreviewInfo();

        mainView();
    }

    @Override
    protected void title() {

    }

    private void mainView() {
        final MainAdapter mAdapter = new MainAdapter(getSupportFragmentManager(), this, mainTabs);
        vpMain.setAdapter(mAdapter);

        vpMain.setOffscreenPageLimit(4);
        vpMain.setAdapter(mAdapter);
        tlMain.setupWithViewPager(vpMain);
        tlMain.setTabMode(TabLayout.MODE_FIXED);
        tlMain.setSelectedTabIndicatorColor(getResources().getColor(R.color.transparency));
        for (int i = 0; i < tlMain.getTabCount(); i++) {
            TabLayout.Tab tab = tlMain.getTabAt(i);
            View tabView = mAdapter.getTabView(i);
            if (tabView != null && tab != null) {
                tab.setCustomView(tabView);
            }
        }

        tlMain.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mAdapter.changeState(true, tab);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                mAdapter.changeState(false, tab);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
    }

    public void getDefaultInfo() {
        WindowManager wm = (WindowManager) this
                .getSystemService(Context.WINDOW_SERVICE);
        if (wm == null) return;
        Display defaultDisplay = wm.getDefaultDisplay();
        int width = defaultDisplay.getWidth();
        App.preferenceUtil.putInt(PreferenceKey.SCREEN_WIDTH, width);
    }

    public void getThraPreviewInfo() {
        showLoadProgresbar();

        LoginModel.DataBean dataBean = Contas.getObject(this, LoginModel.DataBean.class);
        if (dataBean == null) return;

        String uid = dataBean.getId();
        if (uid == null) return;
        String token = dataBean.getAndroidtoken();
        if (token == null) return;

        Call<ThraPreviewModule> call = ApiHelper.thraPreview(uid, token);
        call.enqueue(new Callback<ThraPreviewModule>() {
            @Override
            public void onResponse(Call<ThraPreviewModule> call, Response<ThraPreviewModule> response) {
                ThraPreviewModule body = response.body();
                if (body == null) return;

                String code = body.getCode();
                if (code != null && code.equals("0")) {
                    ThraPreviewModule.DataBean data = body.getData();
                    if (data != null)
                        Contas.putObject(MainActivity.this, data.getClass());
                } else {
                    String msg = body.getMsg();
                    if (msg != null)
                        Log.d(TAG, msg);
                }

                missLoadProgressbar();
            }

            @Override
            public void onFailure(Call<ThraPreviewModule> call, Throwable t) {
                Log.e(TAG, t.toString());
                missLoadProgressbar();
            }
        });
    }
}
